package com.hxzy.controller.anonymous.merchandiser;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.BCrypt;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.exception.ServiceStringException;
import com.hxzy.common.resp.R;
import com.hxzy.controller.anonymous.mapstruct.AnonyMerchandiserConvert;
import com.hxzy.controller.anonymous.merchandiser.req.MerchandiserEditReq;
import com.hxzy.controller.anonymous.merchandiser.req.MerchandiserRegisterReq;
import com.hxzy.controller.anonymous.merchandiser.resp.MerchandiserProcessResp;
import com.hxzy.entity.Merchandiser;
import com.hxzy.service.MerchandiserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

/**
 * @Author wy
 * @Description 商家
 * @Date 2023/9/7 11:05
 * @Version 1.0
 */
@RequestMapping(value = "/anon/merchandiser")
@RestController
@Api(tags = "公共API-商家")
public class AnonyMerchandiserController {


    @Autowired
    private MerchandiserService merchandiserService;

    @ApiOperation(value = "注册")
    @PostMapping(value = "/register")
    public R register(@RequestBody @Valid MerchandiserRegisterReq req){

        if(!req.getLoginPwd().equals(req.getReLoginPwd())){
            return R.build(AckCode.SET_PASSWORD_INVALID);
        }

        //验证账号是否存在
        LambdaQueryWrapper<Merchandiser> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Merchandiser::getLoginName,req.getLoginName());
        long count = this.merchandiserService.count(queryWrapper);
        if(count>0){
            return R.build(AckCode.PHONE_NUMBER_SET_ACCOUNT_INVALID);
        }

        Merchandiser entity = AnonyMerchandiserConvert.INSTANCE.convert(req);
        entity.setStatus("0");
        entity.setCreateBy(entity.getLoginName());
        entity.setLoginPwd(BCrypt.hashpw(req.getLoginPwd()));

        boolean success= this.merchandiserService.save(entity);
        return success?R.ok(): R.build(AckCode.DATABASE_ERROR);
    }


    @ApiOperation(value = "查询商家审核情况")
    @GetMapping(value = "/process/{phone}")
    public R<MerchandiserProcessResp>  searchByPhone(@PathVariable(value = "phone") String phone){
        if(StrUtil.isBlank(phone)){
            throw new ServiceStringException(AckCode.PHONE_NUMBER_NOTNULL.getMsg());
        }
        String reg="0?(13|15|17|18|14|19)[0-9]{9}";
        boolean match = ReUtil.isMatch(reg, phone);
        if(!match){
            throw new ServiceStringException(AckCode.PHONE_NUMBER_ERROR.getMsg());
        }

        MerchandiserProcessResp resp=  this.merchandiserService.searchByPhone(phone);
        return R.okHasData(resp);
    }

    @ApiOperation(value = "重新提交商家信息")
    @PostMapping(value = "/review")
    public R review(@RequestBody @Valid MerchandiserEditReq req){
        Merchandiser db = this.merchandiserService.getById(req.getId());
        if(Objects.isNull(db)){
            return  R.build(AckCode.NOT_FOUND_DATA);
        }

        //如果不是审核不通过，不允许提交
        if(!db.getStatus().equals("2")){
            return R.build(AckCode.MERCHANDISER_ACCOUNT_STATUS_ERROR);
        }

        //把值复制过去
        BeanUtils.copyProperties(req,db);
        //设定为未审核状态
        db.setStatus("0");
        boolean result=this.merchandiserService.updateById(db);
        return result? R.ok(): R.build(AckCode.FAIL);
    }


}
